package cn.coderdream.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 作者: mengpeng
 * 时间: 2019-05-13 - 14:26
 * 用途: 自定义标题
 */
public class MTitleView extends RelativeLayout {
    private Context mContext;
    private int mTitleView_backgroundImage = -1;
    private int mTitleView_backgroundColor = Color.parseColor("#e4e4e4");
    private boolean mTitleView_isWithStatusBarHeight = true;
    private float mTitleView_rightMenu_textSize = sp2px(12);
    private int mTitleView_rightMenu_textColor = Color.parseColor("#ffffff");
    private String mTitleView_rightMenuText = "";
    private int mTitleView_rightMenuIcon = -1;
    private int mTitleView_leftIcon = -1;
    private float mTitleView_leftIcon_textSize = sp2px(12);
    private int mTitleView_leftIcon_textColor = Color.parseColor("#ffffff");
    private String mTitleView_leftIcon_text = "";
    private boolean mTitleView_isShow_leftIcon = true;
    private int mTitleView_content_gravity = 2000;
    private float mTitleView_content_textSize = 16;
    private int mTitleView_content_textColor = Color.parseColor("#ffffff");
    private String mTitleView_content = "";
    private RelativeLayout mTitleView_container;
    private TextView mTitleView_backText;
    private TextView mTitleView_title;
    private TextView mTitleView_menuText;
    private ImageView mTitleView_back;
    private ImageView mTitleView_menu;

    public MTitleView(Context context) {
        this(context, null);
    }

    public MTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        init(attrs, defStyleAttr);

    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.MTitleView, defStyleAttr, 0);
        try {
            int indexCount = typedArray.getIndexCount();
            for (int i = 0; i < indexCount; i++) {
                int index = typedArray.getIndex(i);
                if (index == R.styleable.MTitleView_mTitleView_content) {
                    mTitleView_content = typedArray.getString(index);
                } else if (index == R.styleable.MTitleView_mTitleView_content_textColor) {
                    mTitleView_content_textColor = typedArray.getColor(index, Color.parseColor("#ffffff"));
                } else if (index == R.styleable.MTitleView_mTitleView_content_textSize) {
                    mTitleView_content_textSize = typedArray.getDimension(index, 16);
                } else if (index == R.styleable.MTitleView_mTitleView_content_gravity) {
                    mTitleView_content_gravity = typedArray.getInt(index, 2000);
                } else if (index == R.styleable.MTitleView_mTitleView_isShow_leftIcon) {
                    mTitleView_isShow_leftIcon = typedArray.getBoolean(index, true);
                } else if (index == R.styleable.MTitleView_mTitleView_leftIcon_text) {
                    mTitleView_leftIcon_text = typedArray.getString(index);
                } else if (index == R.styleable.MTitleView_mTitleView_leftIcon_textColor) {
                    mTitleView_leftIcon_textColor = typedArray.getColor(index, Color.parseColor("#ffffff"));
                } else if (index == R.styleable.MTitleView_mTitleView_leftIcon_textSize) {
                    mTitleView_leftIcon_textSize = typedArray.getDimension(index, sp2px(16));
                } else if (index == R.styleable.MTitleView_mTitleView_leftIcon) {
                    mTitleView_leftIcon = typedArray.getResourceId(index, -1);
                } else if (index == R.styleable.MTitleView_mTitleView_rightMenuIcon) {
                    mTitleView_rightMenuIcon = typedArray.getResourceId(index, -1);
                } else if (index == R.styleable.MTitleView_mTitleView_rightMenuText) {
                    mTitleView_rightMenuText = typedArray.getString(index);
                } else if (index == R.styleable.MTitleView_mTitleView_rightMenu_textColor) {
                    mTitleView_rightMenu_textColor = typedArray.getColor(index, Color.parseColor("#ffffff"));
                } else if (index == R.styleable.MTitleView_mTitleView_rightMenu_textSize) {
                    mTitleView_rightMenu_textSize = typedArray.getDimension(index, sp2px(16));
                } else if (index == R.styleable.MTitleView_mTitleView_isWithStatusBarHeight) {
                    mTitleView_isWithStatusBarHeight = typedArray.getBoolean(index, true);
                } else if (index == R.styleable.MTitleView_mTitleView_backgroundColor) {
                    mTitleView_backgroundColor = typedArray.getColor(index, Color.parseColor("#e4e4e4"));
                } else if (index == R.styleable.MTitleView_mTitleView_backgroundImage) {
                    mTitleView_backgroundImage = typedArray.getResourceId(index, -1);
                }
            }
        } catch (Exception e) {
            e.fillInStackTrace();
        } finally {
            getLayout();
            typedArray.recycle();
        }

    }

    private void getLayout() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.m_titleview, this);
        mTitleView_container = view.findViewById(R.id.mTitleView_container);
        mTitleView_backText = view.findViewById(R.id.mTitleView_backText);
        mTitleView_title = view.findViewById(R.id.mTitleView_title);
        mTitleView_menuText = view.findViewById(R.id.mTitleView_menuText);
        mTitleView_back = view.findViewById(R.id.mTitleView_back);
        mTitleView_menu = view.findViewById(R.id.mTitleView_menu);

        //1.背景
        if (mTitleView_backgroundImage > -1) {
            mTitleView_container.setBackgroundResource(mTitleView_backgroundImage);
        } else if (mTitleView_backgroundColor != 0) {
            mTitleView_container.setBackgroundColor(mTitleView_backgroundColor);
        }
        ViewGroup.LayoutParams layoutParams = mTitleView_container.getLayoutParams();
        if (mTitleView_isWithStatusBarHeight) {
            int statusBarHeight = getStatusBarHeight(mContext);
            layoutParams.height = statusBarHeight + dp2px(46);
        } else {
            layoutParams.height = dp2px(46);
        }
        mTitleView_container.setLayoutParams(layoutParams);
        //2.返回图标
        if (mTitleView_isShow_leftIcon) {
            mTitleView_back.setVisibility(VISIBLE);
        } else {
            mTitleView_back.setVisibility(GONE);
        }
        if (mTitleView_leftIcon != -1) {
            mTitleView_back.setImageResource(mTitleView_leftIcon);
        }
        //3.标题内容
        if (!TextUtils.isEmpty(mTitleView_content)) {
            mTitleView_title.setText(mTitleView_content);
            mTitleView_title.setTextSize(mTitleView_content_textSize);
            mTitleView_title.setTextColor(mTitleView_content_textColor);
        }
        //4.菜单icon
        if (mTitleView_rightMenuIcon != -1) {
            mTitleView_menu.setImageResource(mTitleView_rightMenuIcon);
            mTitleView_menu.setVisibility(VISIBLE);
        } else {
            mTitleView_menu.setVisibility(GONE);
        }
        //5.菜单文字
        if (!TextUtils.isEmpty(mTitleView_rightMenuText)) {
            mTitleView_menu.setVisibility(GONE);
            mTitleView_menuText.setVisibility(VISIBLE);
            mTitleView_menuText.setText(mTitleView_rightMenuText);
            mTitleView_menuText.setTextColor(mTitleView_rightMenu_textColor);
            mTitleView_menuText.setTextSize(mTitleView_rightMenu_textSize);
        } else {
            mTitleView_menuText.setVisibility(GONE);
        }
        //6.左边text
        if (!TextUtils.isEmpty(mTitleView_leftIcon_text)) {
            mTitleView_backText.setText(mTitleView_leftIcon_text);
            mTitleView_backText.setTextColor(mTitleView_leftIcon_textColor);
            mTitleView_backText.setTextSize(mTitleView_leftIcon_textSize);
            mTitleView_backText.setVisibility(VISIBLE);
        } else {
            mTitleView_backText.setVisibility(GONE);
        }

    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     */
    public int sp2px(float spValue) {
        float fontScale = getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 将dp值转换为px值
     */
    public int dp2px(float dpValue) {
        float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5F);
    }

    /**
     * 获取状态栏的高度
     */
    public int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
